<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.wzu.pojo.BorrowRecord" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑借阅记录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .error-message {
            color: red;
            margin-bottom: 10px;
        }
        form {
            background-color: #f2f2f2;
            padding: 20px;
            border-radius: 5px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"], input[type="date"], select, input[type="number"] {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
        .back-link {
            display: inline-block;
            margin-top: 10px;
            color: #2196F3;
            text-decoration: none;
        }
        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>编辑借阅记录</h1>
    <%
        String error = (String) request.getAttribute("error");
        if (error != null) {
    %>
    <div class="error-message"><%= error %></div>
    <%
        }
        BorrowRecord record = (BorrowRecord) request.getAttribute("record");
        if (record != null) {
    %>
    <form action="borrowRecord" method="post">
        <input type="hidden" name="action" value="update">
        <input type="hidden" name="record_id" value="<%= record.getRecord_id() %>">

        <label for="book_id">图书:</label>
        <select id="book_id" name="book_id" required>
            <option value="">请选择图书</option>
            <!-- 实际应用中，这里应该通过后端动态获取图书列表 -->
            <option value="1" <%= record.getBook_id() == 1 ? "selected" : "" %>>Java编程思想</option>
            <option value="2" <%= record.getBook_id() == 2 ? "selected" : "" %>>Python数据分析实战</option>
            <option value="3" <%= record.getBook_id() == 3 ? "selected" : "" %>>数据结构与算法</option>
        </select>

        <label for="reader_id">读者:</label>
        <select id="reader_id" name="reader_id" required>
            <option value="">请选择读者</option>
            <!-- 实际应用中，这里应该通过后端动态获取读者列表 -->
            <option value="1" <%= record.getReader_id() == 1 ? "selected" : "" %>>张三</option>
            <option value="2" <%= record.getReader_id() == 2 ? "selected" : "" %>>李四</option>
            <option value="3" <%= record.getReader_id() == 3 ? "selected" : "" %>>王五</option>
        </select>

        <label for="borrow_date">借阅日期:</label>
        <input type="date" id="borrow_date" name="borrow_date"
               value="<%= record.getBorrow_date() != null ? record.getBorrow_date().toString() : "" %>" required>

        <label for="due_date">应归还日期:</label>
        <input type="date" id="due_date" name="due_date"
               value="<%= record.getDue_date() != null ? record.getDue_date().toString() : "" %>" required>

        <label for="return_date">实际归还日期:</label>
        <input type="date" id="return_date" name="return_date"
               value="<%= record.getReturn_date() != null ? record.getReturn_date().toString() : "" %>">

        <label for="is_renewed">是否续借:</label>
        <select id="is_renewed" name="is_renewed" required>
            <option value="false" <%= !record.isIs_renewed() ? "selected" : "" %>>否</option>
            <option value="true" <%= record.isIs_renewed() ? "selected" : "" %>>是</option>
        </select>

        <label for="fine">罚金:</label>
        <input type="number" id="fine" name="fine" min="0" step="0.01"
               value="<%= record.getFine() %>" required>

        <input type="submit" value="更新记录">
    </form>
    <%
    } else {
    %>
    <div style="text-align: center; color: red;">找不到借阅记录</div>
    <%
        }
    %>
    <a href="borrowRecord?action=list" class="back-link">返回列表</a>
</div>
</body>
</html>